// 题目：
// 循环打印
// 红灯3秒后变成绿灯
// 绿灯5秒后变成黄灯
// 黄灯2秒后变成红灯

// 实现：
class TrafficLight {
  static rules(color, delay, next) {
    console.log(`${color}, ${delay}s => ${next}`);
    this.timer = setTimeout(() => {
      this.start(next);
    }, delay * 1000);
  }
  start(light) {
    if(light === 'green') {
      TrafficLight.rules.apply(this, ['green', 5, 'yellow']);
    } else if(light === 'yellow') {
      TrafficLight.rules.apply(this, ['yellow', 2, 'red']);
    } else {
      TrafficLight.rules.apply(this, ['red', 3, 'green']);
    }
  }
  stop() {
    clearTimeout(this.timer);
  }
}
const light = new TrafficLight();
light.start();